package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dictionaries;
import cn.wolfcode.crm.domain.DictionariesItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionariesService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dictionaries")
public class DictionariesController {

    @Autowired
    private IDictionariesService dictionariesService;
    
    @RequestMapping("/view")
    public String dictionaries(){

        return "system/dictionaries";
    }

    @RequiresPermissions(value = {"dictionaries:query", "数据字典列表"}, logical = Logical.OR)
    @RequestMapping("/query")
    @ResponseBody
    public Object query(QueryObject qo){
        return dictionariesService.query(qo);
    }


    @RequiresPermissions(value = {"dictionaries:save", "数据字典新增/编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Dictionaries entity){
        try {
            dictionariesService.saveOrUpdate(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }



    //查询所有数据字典对象
    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectAll(){
        return dictionariesService.listAll();
    }


    //删除数据字典
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        dictionariesService.delete(id);
        return new JsonResult();
    }


    //获取数据字典
    @RequestMapping("getDictionaries")
    @ResponseBody
    public List<DictionariesItem> getDictionaries(String str){
        //获取实施方式
        List<DictionariesItem>  dictionariesItems = dictionariesService.selectItemName(str);
        return dictionariesItems;
    }
}
